<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>个人简历</title>
  <style>
    table,
    th,
    td {
      border: 1px solid #000;
      border-collapse: collapse;
      text-align: center;
      padding: 15px;
      margin-bottom: 10px;
    }

    thead {
      background-color: #DEF1B8;
    }

    #headpic {
      width: 100px;
      height: 100px;
    }
  </style>
</head>

<body>
  <table>
    <thead>
      <tr>
        <th colspan="5">个人简历</th>
      </tr>
    </thead>
    <tbody id="data">
      <tr>
        <td width="15%">姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</td>
        <td></td>
        <td width="15%">性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别:</td>
        <td></td>
        <th rowspan="4" width="25%">照片</th>
      </tr>
      <tr>
        <td>婚姻状况:</td>
        <td></td>
        <td>出生年月:</td>
        <td></td>
      </tr>
      <tr>
        <td>民&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;族:</td>
        <td></td>
        <td>政治面貌:</td>
        <td></td>
      </tr>
      <tr>
        <td>身&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高:</td>
        <td></td>
        <td>学&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;历:</td>
        <td></td>
      </tr>
    </tbody>
  </table>
  <select id="selectUser">
    <option>---请选择用户---</option>
  </select>

  <script>
    //AJAX-页面加载完成后把数据库中所有的用户名动态绑定到下拉菜单
    window.onload = function () {
      let xhr
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest()
      } else {
        xhr = new ActiveXObject('Microsoft.XMLHTTP')
      }
      xhr.open('GET', 'http://localhost:8888/AjaxTeach/users/all', true)
      xhr.send(null)
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          let jsonStr = xhr.responseText
          //把后台发送的json字符串转换成js对象(数组对象)
          const users = JSON.parse(jsonStr)
          //遍历数组
          users.forEach(user => {
            //动态绑定到下拉菜单中
            const option = document.createElement('option')
            option.innerHTML = user.name
            option.value = user.id
            selectUser.appendChild(option)
          })
        }
      }
    }

    //下拉菜单发生改变事件
    selectUser.onchange = function () {
      let xhr
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest()
      } else {
        xhr = new ActiveXObject('Microsoft.XMLHTTP')
      }
      xhr.open('GET', 'http://localhost:8888/AjaxTeach/users/find?id=' + this.value, true)
      xhr.send(null)
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          let jsonStr = xhr.responseText
          //把后台发送的json字符串转换成js对象(单个对象)
          const user = JSON.parse(jsonStr)
          //动态在页面上渲染数据
          data.innerHTML = `<tr>
        <td width="15%">姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</td>
        <td>${user.name}</td>
        <td width="15%">性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别:</td>
        <td>${user.sex}</td>
        <th rowspan="4" width="25%">
          <img id="headpic" src="../img/${user.head}.webp"/>  
        </th>
      </tr>
      <tr>
        <td>婚姻状况:</td>
        <td>${user.marriage}</td>
        <td>出生年月:</td>
        <td>${user.birthday}</td>
      </tr>
      <tr>
        <td>民&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;族:</td>
        <td>${user.nation}</td>
        <td>政治面貌:</td>
        <td>${user.political}</td>
      </tr>
      <tr>
        <td>身&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高:</td>
        <td>${parseInt(user.height)}</td>
        <td>学&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;历:</td>
        <td>${user.education}</td>
      </tr>`
        }
      }
    }

  </script>
</body>

</html>